/*
 * TestThread.cpp
 *
 *  Created on: 8 déc. 2013
 *      Author: Raphaël
 */

#include <utility>
#include "Thread.h"
#include "ThreadException.h"

namespace raph
{
/*-------------------------------------------------------------------
 constructor/destructor
 -------------------------------------------------------------------*/
Thread::Thread()
	{

	}

Thread::Thread(Thread& other)
	{
	if (!m_thread.joinable())
		{
		m_thread = std::move(other.m_thread);
		}
	}

Thread& Thread::operator=(Thread& other)
	{
	if (this != &other)
		{
		if (!m_thread.joinable())
			{
			m_thread = std::move(other.m_thread);
			}
		}

	return *this;
	}

Thread::~Thread()
	{

	}
/*-------------------------------------------------------------------
 public methods
 -------------------------------------------------------------------*/
void Thread::start()
	{
	m_thread = std::thread([this]
		{run();});
	}

void Thread::join()
	{
	if (!m_thread.joinable())
		{
		throw ThreadException("the thread cannot be joined", __LINE__, __FILE__);
		}
	m_thread.join();
	}

void Thread::detach()
	{
	if (!m_thread.joinable())
		{
		throw ThreadException("the thread cannot be detached", __LINE__, __FILE__);
		}
	m_thread.detach();

	}
} /* namespace raph */
